function AjaxData(url, postData)
{
	this.url = url
	if(typeof(postData)=="object")
	{
		this.postData=""
		for(var i in postData)
		{
//			this.postData+="&"+escape(i).replace(/\+/g, "%2B")+"="+escape(postData[i]).replace(/\+/g, "%2B")
			this.postData+="&"+encodeURIComponent(i).replace(/\+/g, "%2B")+"="+encodeURIComponent(postData[i]).replace(/\+/g, "%2B")
		}
		this.postData = this.postData.substring(1)
	}
	else
	{
		this.postData = postData
	}
	this._status = "pending"
}

AjaxData.prototype.getMode = function()
{
	if(this.postData==null)
	{
		return "GET"
	}
	else
	{
		return "POST"
	}
}
AjaxData.prototype._getXHR = function()
{
	if(window.XMLHttpRequest) // Firefox
	{
		return new XMLHttpRequest();
	}
	else if(window.ActiveXObject) // Internet Explorer
	{
		return new ActiveXObject("Microsoft.XMLHTTP");
	}
	else
	{
		alert("Votre navigateur ne supporte pas les objets XMLHTTPRequest...");
		return null
	}
}
AjaxData.prototype.sendAsync = function()
{
	var xhr = this._getXHR()
	xhr.open(this.getMode(), this.url, true);

	var this_object = this
	xhr.onreadystatechange = function()
	{
		if(xhr.readyState == 4)
		{
			if(this_object.callBack!=null)
			{
				this_object.callBack(xhr.responseText)
			}
			this_object.manager._nb_sended--
			this_object.status = "deleted"
		}
	}
	xhr.setRequestHeader("Content-type", "application/x-www-form-urlencoded; charset=UTF-8")
	this_object.status = "sending"
	xhr.send(this.postData);	
}
AjaxData.prototype.sendSync = function()
{
	var xhr = this._getXHR()
	xhr.open(this.getMode(), this.url, false);
	xhr.setRequestHeader("Content-type", "application/x-www-form-urlencoded; charset=UTF-8")
	xhr.send(this.postData);
	if(xhr.readyState == 4)
	{
		return xhr.responseText;
	}
	return null
}

function AjaxManager()
{
	this.max_send = 5;
	
	this._nb_sended = 0;
	this._objects = new Array();
	this._buffer = new Array();

	var this_object = this
	setInterval(function()
	{this_object._cleanObjects()}, 1000)
	setInterval(function()
	{this_object._flushBuffer()}, 100)
}

AjaxManager.prototype._cleanObjects = function()
{
	while(this._objects.length>0 && this._objects[0].status == "deleted")
	{
		this._objects.shift()
	}
}
AjaxManager.prototype._flushBuffer = function()
{
	if(this._buffer.length>0 && this._nb_sended < this.max_send)
	{
		this._nb_sended++
		var ad = this._buffer.shift()

		this._objects.push(ad)
		ad.sendAsync()
	}
}

AjaxManager.prototype.sendAsync = function(url, callBack, postData, prioritaire)
{
	var ad = new AjaxData(url, postData)
	ad.callBack = callBack;
	ad.manager = this
	
	if(prioritaire==null)
	{
		prioritaire = false;
	}
	
	if(!prioritaire)
	{
		this._buffer.push(ad);
	}
	else
	{
		this._buffer.unshift(ad);
	}
}

AjaxManager.prototype.sendSync = function(url, postData)
{
	var ad = new AjaxData(url, postData)
	return ad.sendSync()
}

AjaxManager.prototype.buildDatas = function(form)
{
	var ch = new Array();
	
	var tag = new Array("INPUT", "TEXTAREA", "SELECT")
	
	for(var t in tag)
	{
		var elems = form.getElementsByTagName(tag[t])
		for(var i=0; i<elems.length; i++)
		{
			if(elems[i].name==null || elems[i].name=="") continue;
			if(elems[i].type.toUpperCase() == 'RADIO')
			{
				if(elems[i].checked)
				{
					ch.push(encodeURIComponent(elems[i].name).replace(/\+/g, "%2B")+"="+encodeURIComponent(elems[i].value).replace(/\+/g, "%2B"));
				}
			}
			else if(elems[i].type.toUpperCase() == 'CHECKBOX')
			{
				if(elems[i].checked)
				{
					ch.push(encodeURIComponent(elems[i].name).replace(/\+/g, "%2B")+"="+(elems[i].value==''?'on':encodeURIComponent(elems[i].value).replace(/\+/g, "%2B")));
				}
				else
				{
					ch.push(encodeURIComponent(elems[i].name).replace(/\+/g, "%2B")+"=");
				}
			}
			else
			{
				ch.push(encodeURIComponent(elems[i].name).replace(/\+/g, "%2B")+"="+encodeURIComponent(elems[i].value).replace(/\+/g, "%2B"));
			}
		}
	}
	return ch.join("&")
}

AjaxManager.prototype.eval = function(code)
{
	try
	{
		var ajax_return_data = null
		eval(code)
		return ajax_return_data;
	}
	catch(e)
	{
		alert("ERROR ajax : \n\n"+e.message+"\n\n"+code);
	}
}

var ajax = new AjaxManager();